﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Dao.Interface;
using NHibernate.Criterion;

namespace Dao
{
    public class ReplyRepository : Repository<Reply>, IReplyRepository
    {
        /// <summary>
        /// 获取回复分页集合
        /// </summary>
        /// <param name="pageID">共享页ID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">总页数</param>
        /// <returns></returns>
        public IQueryable<Reply> LoadAllReply(int pageID, int pageIndex, int pageSize, out int pageCount)
        {
            var replyList = this.LoadAll().Where(i => i.Page.ID == Convert.ToInt32(pageID));
            int totalCount = replyList.Count();
            if (totalCount % pageSize == 0)
            {
                pageCount = totalCount / pageSize;
            }
            else
            {
                pageCount = totalCount / pageSize + 1;
            }
            replyList = replyList.OrderByDescending(o => o.Time);
            replyList = replyList.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return replyList;
        }

        /// <summary>
        /// 查询页总回复数
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <returns></returns>
        public int SelectReplyCount(object pageID)
        {
            return (int)this.Session.CreateCriteria(typeof(Reply))
                        .CreateCriteria("Page").Add(Restrictions.Eq("ID", Convert.ToInt32(pageID)))
                        .SetProjection(Projections.RowCount()).UniqueResult();
        }
    }
}
